import io
import json
import copy
import random
import struct
import uuid
from typing import List
from typing import Union
from sqlalchemy import and_, desc
from openpyxl import load_workbook
import pickle
import models
from models.course.detailed import Detailed
from models.facility.facility import Facility


warnings: dict = {
    "Status": {
        "Key": "DM8040",
        "Option": {
            1: "未运行", 2: "初始化中", 4: "初始化完成", 8: "自动运行中", 16: "设备报警中"
        }
    },
    "Alarm": []
}


parm: dict = {
    "DM8000": {
        "ROW": 7,
        "COLUMN": 6,
        "TITLE": "轴报警"
    },
    # #############################################
    "DM8001": {
        "ROW": 7,
        "COLUMN": 7,
        "TITLE": "轴限位报警"
    },
    "DM8002": {
        "ROW": 7,
        "COLUMN": 8,
        "TITLE": "轴限位报警"
    },
    # #############################################
    "DM8003": {
        "ROW": 7,
        "COLUMN": 9,
        "TITLE": "轴限位报警"
    },
    "DM8004": {
        "ROW": 7,
        "COLUMN": 10,
        "TITLE": "其他报警"
    },
    "DM8005": {
        "ROW": 7,
        "COLUMN": 11,
        "TITLE": "其他报警"
    },
    "DM8006": {
        "ROW": 7,
        "COLUMN": 12,
        "TITLE": "其他报警"
    },
    "DM8007": {
        "ROW": 7,
        "COLUMN": 13,
        "TITLE": "其他报警"
    },
    "DM8008": {
        "ROW": 7,
        "COLUMN": 14,
        "TITLE": "其他报警"
    },
    "DM8009": {
        "ROW": 7,
        "COLUMN": 15,
        "TITLE": "其他报警"
    },
    # #############################################
    "DM8010": {
        "ROW": 7,
        "COLUMN": 16,
        "TITLE": "物料感应器报警"
    },
    "DM8011": {
        "ROW": 7,
        "COLUMN": 17,
        "TITLE": "物料感应器报警"
    },
    "DM8012": {
        "ROW": 7,
        "COLUMN": 18,
        "TITLE": "物料感应器报警"
    },
    "DM8013": {
        "ROW": 7,
        "COLUMN": 19,
        "TITLE": "物料感应器报警"
    },
    "DM8014": {
        "ROW": 7,
        "COLUMN": 20,
        "TITLE": "物料感应器报警"
    },
    "DM8015": {
        "ROW": 7,
        "COLUMN": 21,
        "TITLE": "物料感应器报警"
    },
    "DM8016": {
        "ROW": 7,
        "COLUMN": 22,
        "TITLE": "物料感应器报警"
    },
    "DM8017": {
        "ROW": 7,
        "COLUMN": 23,
        "TITLE": "物料感应器报警"
    },
    "DM8018": {
        "ROW": 7,
        "COLUMN": 24,
        "TITLE": "物料感应器报警"
    },
    "DM8019": {
        "ROW": 7,
        "COLUMN": 25,
        "TITLE": "物料感应器报警"
    },
    # #############################################
    "DM8020": {
        "ROW": 7,
        "COLUMN": 26,
        "TITLE": "气缸感应器报警"
    },
    "DM8021": {
        "ROW": 7,
        "COLUMN": 27,
        "TITLE": "气缸感应器报警"
    },
    "DM8022": {
        "ROW": 7,
        "COLUMN": 28,
        "TITLE": "气缸感应器报警"
    },
    "DM8023": {
        "ROW": 7,
        "COLUMN": 29,
        "TITLE": "气缸感应器报警"
    },
    "DM8024": {
        "ROW": 7,
        "COLUMN": 30,
        "TITLE": "气缸感应器报警"
    },
    "DM8025": {
        "ROW": 7,
        "COLUMN": 31,
        "TITLE": "气缸感应器报警"
    },
    "DM8026": {
        "ROW": 7,
        "COLUMN": 32,
        "TITLE": "气缸感应器报警"
    },
    "DM8027": {
        "ROW": 7,
        "COLUMN": 33,
        "TITLE": "气缸感应器报警"
    },
    "DM8028": {
        "ROW": 7,
        "COLUMN": 34,
        "TITLE": "气缸感应器报警"
    },
    "DM8029": {
        "ROW": 7,
        "COLUMN": 35,
        "TITLE": "气缸感应器报警"
    },
}


if __name__ == '__main__':
    print(uuid.uuid4())

    # all_s = [16784, 0]
    # temp_list = [all_s[0] >> 8 & 0xff, all_s[0] & 0xff, all_s[1] >> 8 & 0xff, all_s[1] & 0xff]
    # resistance_mini, = struct.unpack('>f', struct.pack("4B", *temp_list))
    # resistance_mini = round(resistance_mini, 2)
    #
    # print(resistance_mini)


    # with open("XXX.xlsx", "rb") as file:
    #     date_xlsx: bytes = file.read()
    # stream_xlsx = io.BytesIO(date_xlsx)
    # workbook = load_workbook(stream_xlsx)
    # sheet = workbook['报警信息']
    #
    # temp_list: list = []
    # for key, value in parm.items():
    #     temp_dict: dict = {
    #         "Key": value["TITLE"],
    #         "Io": key,
    #         "Option": {}
    #     }
    #     for index in range(16):
    #         error: Union[str, None] = sheet.cell(row=value["ROW"] + index + 1, column=value["COLUMN"]).value
    #         if error is not None:
    #             temp_dict["Option"][2 ** index] = error
    #     if len(temp_dict.get("Option")) != 0:
    #         temp_list.append(copy.deepcopy(temp_dict))


    warnings["Alarm"] = []
    byte: bytes = pickle.dumps(warnings)
    with open("warns/init.bin", "wb") as file:
        pickle.dump(warnings, file)
    with open("warns/init.bin", "rb") as file:
        xxc = pickle.load(file)
    print(xxc)
    session = models.SessionFactory()
    db_facility: Union[Facility, None] = session.query(Facility).filter(Facility.id == 1).first()
    # db_facility.alarms = byte
    #
    # session.commit()
    #
    # print(db_facility)
